package com.dgut.shopManage.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dgut.common.util.ExtAjaxResponse;
import com.dgut.common.util.ExtPageable;
import com.dgut.shopManage.entity.Apply;
import com.dgut.shopManage.entity.dto.ApplyDTO;
import com.dgut.shopManage.entity.dto.ApplyQueryDTO;
import com.dgut.shopManage.service.IApplyService;


@Controller
@RequestMapping("/apply")
public class ApplyContreller {
	@Autowired
	private IApplyService applyService;
	
	@RequestMapping("/applySaveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(ApplyDTO dto) 
	{
		try {
			System.out.println("con:"+dto);
			//dto.setShopId(dto.getId());
			applyService.saveOrUpdate(dto);
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
		
	}
	@RequestMapping("/applyDelete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long id) 
	{	
		try {
		Apply apply =  applyService.findOne(id);
		if(null!=apply) {
			applyService.delete(apply);
		}
		return new ExtAjaxResponse(true,"操作成功！");
		}
		catch (Exception e) {
		return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	
	@RequestMapping("/applyFindOne")
	public @ResponseBody Apply findOne(@RequestParam Long id) 
	{
		Apply apply =  applyService.findOne(id);
		
		return apply;
	}
	
	
	@RequestMapping("/applyFindAll")
	public @ResponseBody List<ApplyDTO> findAll() 
	{
		return applyService.findAll();
	}
	
	@RequestMapping("/applyFindPage")
	public @ResponseBody Page<ApplyDTO> findPage(ApplyQueryDTO applyQueryDTO,ExtPageable pageable) 
	{
		Page<ApplyDTO> page =  applyService.findAll(applyQueryDTO.getSpecification(applyQueryDTO), pageable.getPageable());
		return page;
	}
	
	@RequestMapping("/applyFindPage2")
	public @ResponseBody Page<ApplyDTO> findByShop(Long shopId,ExtPageable pageable) 
	{
		if(shopId!=null) {
			return applyService.findByShop(shopId,pageable.getPageable());
		}else {
			return null;
		}
	}
	@RequestMapping("/audit")
	public @ResponseBody ExtAjaxResponse audit(Long id) {
		try {
		applyService.audit(id);
		Apply apply =  applyService.findOne(id);
		applyService.delete(apply);
		return new ExtAjaxResponse(true,"操作成功！");
		}catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	@RequestMapping("/pass")
	public @ResponseBody ExtAjaxResponse pass(Long id) {
		try {
		applyService.audit(id);
		applyService.pass(id);
		Apply apply =  applyService.findOne(id);
		applyService.delete(apply);
		return new ExtAjaxResponse(true,"操作成功！");
		}catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	@RequestMapping("/passNo")
	public @ResponseBody ExtAjaxResponse passNo(Long id) {
		try {
			applyService.passNo(id);
		Apply apply =  applyService.findOne(id);
		applyService.delete(apply);
		return new ExtAjaxResponse(true,"操作成功！");
		}catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
}
